﻿<UserControl x:Class="WPFEdit.Views.TreeWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vm="clr-namespace:WPFEdit.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ContextMenu x:Key="ItemContextMenu">
            <ContextMenu.ItemContainerStyle>
                <Style  TargetType="{x:Type MenuItem}">
                    <Setter Property="Header" Value="{Binding Name}" />
                    <Setter Property="ItemsSource" Value="{Binding SubMenu}" />
                    <Setter Property="Command" Value="{Binding Command}" />
                    <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.DataContext}" />
                    <Setter Property="IsEnabled" Value="{Binding Enabled}" />
                </Style>
            </ContextMenu.ItemContainerStyle>
        </ContextMenu>
    </UserControl.Resources>
    <DockPanel LastChildFill="True">
        <Label Name="WindowTextBlock" Content="{Binding Title}" DockPanel.Dock="Top" Margin="10,10,0,0" Visibility="{Binding TitleVisibility}" />
        
        <TreeView  Name="WindowTreeView" Margin="10" ItemsSource="{Binding TreeItems}">

            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Setter Property="IsEnabled" Value="True" />
                    <EventSetter Event="MouseDoubleClick" Handler="OnItemDoubleClick" />
                    <EventSetter Event="MouseRightButtonDown" Handler="OnItemRightClick" />
                    <Setter Property="ContextMenu" Value="{DynamicResource ItemContextMenu}" />
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.Resources>
                <HierarchicalDataTemplate 
                    DataType="{x:Type vm:TreeViewItem}" 
                    ItemsSource="{Binding Children}"
                    >
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Margin="3,0" Source="{Binding ImagePath}" />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </DockPanel>
</UserControl>
